<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/mb/jsp/common.jsp" %>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的订单</title>
    <style>
        .bmInfo1 > div > div {
            margin: 0 1%;
        }

        .checkboxFive label {
            width: 17px;
            height: 17px;
        }

        .checkboxFive label:after {
            width: 10px;
            height: 5px;
            top: 3px;
            left: 3px;
        }

        .pbm_date {
            border: none;
        }
    </style>
</head>
<body ontouchstart="">
<div class="detailTop pf w100 of">
    <div class="w100 of tc c3 f18">
        <span class="pa" style="left: 5px;" onclick="javascript:history.back();"><img src="mb/images/top_back.png" style="margin-top: 4px;height: 40px;width: 40px;"/></span>
        我的订单
    </div>
</div>

<div class="container db jz" style="padding-bottom: 0;padding-top: 41px;">

    <div id="container">

        <div id="resText"></div><!--resText-->
        <div id="loading" class="w100">
            <img src="mb/images/loading.gif" alt="正在加载" class="db jz hauto" style="width: 15px;">
            <div class="w100 tc f10 c9">正在加载</div>
        </div>
    </div>

    <section class="w100" style="height: 66px;"></section>
    <%--<div class="detailBottm pf w100 of">--%>
        <%--<button onclick="sendOrder();" class="db w100 f18 cf f18 tc">申请发货</button>--%>
    <%--</div>--%>

</div>

<script>

    var curpage = 1;
    var total = 1;

    /**
     * 加载新数据
     */
    function getData() {
        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 1750, curpage: curpage}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg();
            },
            success: function (data) {
                if (data.state == 1) {
                    // 刷新页面
                    total = data.total;
                    refreshPage(data.list);
                } else {
                    // 其他错误弹出提示
                    showToast(data.info);
                }
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
    }

    function getItemHtml(data) {
    	console.log(data);
        // state 订单状态(0 取消 1待付款 2已付款  3可申请发货  4已申请发货 5发货  6签收)
        // owestate   0正常　1抵押中
        var stateHtml = '<span class="ya">未</span>';
        var time1 = '', time2 = '';
        time1 = '投资日期 ' + data.ctimeAbout.split(" ")[0];
        switch (data.state) {
            case '0':
                time2 = '取消日期 ' + dataToString(data.canceltime, "yyyy-MM-dd");
                stateHtml = '<span class="ya">取</span>';
                break;
            case '1':
                time2 = '';
                stateHtml = '<span class="ya">待</span>';
                break;
            case '2':
                time2 = '付款日期 ' + data.starttimeAbout.split(" ")[0];
                stateHtml = '<span class="ya">付</span>';
                break;
            case '3':
                time2 = '可申日期 ' + data.applitimeAbout.split(" ")[0];
                stateHtml = '<span class="ya">可</span>';
                break;
            case '4':
                time2 = '申请日期 ' + data.appliedtimeAbout.split(" ")[0];
                stateHtml = '<span class="ya">申</span>';
                break;
            case '5':
                time2 = '发货日期 ' + data.couriertimeAbout.split(" ")[0];
//                stateHtml = ' <input type="checkbox" name="checkbox" value="' + data.orderid + '" id="' + data.orderid + '"/>'
//                        + '<label for="' + data.orderid + '"></label>';

                stateHtml = '<span class="ya">发</span>';
                break;
            case '6':
                time2 = '签收日期 ' + data.arrivetimeAbout.split(" ")[0];;
                stateHtml = '<span class="ya">收</span>';
                break;
            default:
                break;
        }

        var html = ''
                + '<section class="proSection pr w100 bcf" onclick="toOrderDetail(' + data.orderid + ');">'
                + '<div class="w100 of clearfix">'
                + '<div class="checkboxFive fl" style="margin: 30px 7px;">'
                + stateHtml
                + '</div>'
                + '<div class="fl" style="width: 86%">'
                + '<h5 class="f16 c3 lh16 fb" style="margin-top: 18px;">' + data.projectname + '</h5>'
                + '<div class="bmInfo1 clearfix of w100">'
                + '<div style="width: 37%">'
                + '<div>'
                + '<h6 class="f12 c6">价格</h6>'
                + '<p class="cred f18">' + data.packageprice + '</p>'
                + '</div>'
                + '</div>'
                + '<div class="pr" style="width:26%">'
                + '<div class="pa h100" style="width: 1px;background: #EBEBEB;right: 0.5px;"></div>'
                + '<div class="pa h100" style="width: 1px;background: #EBEBEB;left: 0.5px;"></div>'
                + '<div class="tc">'
                + '<h6 class="f12 c6">数量(件)</h6>'
                + '<p class="c6 f18">' + data.ordernum + '</p>'
                + '</div>'
                + '</div>'
                + '<div style="width: 37%">'
                + '<div>'
                + '<h6 class="f12 c6">资产</h6>'
                + '<p class="cred f18">' + data.totalrmb + '</p>'
                + '</div>'
                + '</div>'
                + '</div>'
                + '<div class="pbm_date of clearfix f12 c9">'
                + '<div class="fl tl">' + time1 + '</div>'
                + '<div class="fr tr">' + time2 + '</div>'
                + '</div>'

                + '</div>'
                + '</div>'
                + '</section>'
                + '';

        return html;
    }

    /**
     * 刷新列表页面
     * @param data
     */
    function refreshPage(data) {
        var html = '<div id="resText"></div>'
                + '<div id="loading" class="w100">'
                + '<img src="mb/images/loading.gif" alt="正在加载" class="db jz hauto" style="width: 15px;">'
                + '<div class="w100 tc f10 c9">正在加载</div>'
                + '</div>';
        $("#container").html(html);

        if (data == null) {
            showToast("暂无提现记录！");
            return;
        }

        html = '';

        for (var i = 0; i < data.length; i++) {
            html += getItemHtml(data[i])
        }

        $("#resText").before($(html));
    }

    $(function () {
        $(window).bind('scroll', function () {
            show()
        });
    });

    function show() {
        if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
            loadNext();
        }
    }

    /**
     * 加载下一页数据
     */
    function loadNext() {
        if (curpage < total) {
            curpage++;

            $.ajax({
                url: "PlugServlet",
                data: JSON.stringify({m: 1750, curpage: curpage}),
                type: 'post',
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                beforeSend: function () {
                    $("#loading").show();
                },
                success: function (data) {
                    if (data.state == 1) {
                        // 刷新页面
                        total = data.total;
                        // 加载更多
                        if (data.list != null) {
                            var html = '';
                            for (var i = 0; i < data.length; i++) {
                                html += getItemHtml(data[i])
                            }

                            $("#resText").before($(html));
                        }
                    } else {
                        // 其他错误弹出提示
                        showToast(data.info);
                    }
                },
                error: function (data) {
                    showToast(data.info);
                },
                complete: function () {
                    $("#loading").hide();
                }
            });
        }
    }

    curpage = 1;
    getData();

    // TODO: 申请发货
    function sendOrder() {
        var orderidlist = "";

        var chks = $("input[name='checkbox']:checkbox:checked");
        for (var i = 0; i < chks.length; i++) {
            orderidlist += chks[i].value + ",";
        }
        if (orderidlist.length > 0) {
            orderidlist = orderidlist.substring(0, orderidlist.length - 1);

            // 发货申请 跳转到填写地址页面
            window.location.href = 'mb/jsp/myAddress.jsp?orderidlist=' + orderidlist;
        } else {
            showToast("请先选择要发货的订单！")
        }
    }

    function toOrderDetail(orderid) {
        window.location.href = 'mb/jsp/zhongchouOrderDetail.jsp?orderid=' + orderid;
    }

</script>

</body>
</html>
